package com.mashang.bookkeepingbook.controller;

import com.mashang.bookkeepingbook.pojo.*;
import com.mashang.bookkeepingbook.pojo.entity.Bill;
import com.mashang.bookkeepingbook.pojo.entity.Data;
import com.mashang.bookkeepingbook.pojo.entity.ExpensesIncome;
import com.mashang.bookkeepingbook.pojo.entity.Result;
import com.mashang.bookkeepingbook.service.BillService;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;
@Slf4j
@RestController
public class BillController {
    @Autowired
    private BillService billService;

    /**
     * 添加账单
     * @return
     */
    @PostMapping("/bill")
    public Result addBill(@RequestParam("amount") Double amount, @RequestParam("billTime") String billTime, @RequestParam("categoryId") Integer categoryId, @RequestParam("remark") String remark, @RequestParam("type") String type, @RequestParam("userId") Integer userId) {
        log.info("参数：:{},{},{},{},{},{}", amount, billTime, categoryId, remark, type, userId);

        // 日期格式化对象 线程安全 （常用格式 2020-01-01 00:00:00）
        DateTimeFormatter localDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime strParseLocalDateTime = null;
        try {
            // 字符串转LocalDateTime
            strParseLocalDateTime = LocalDateTime.parse(billTime, localDateTimeFormatter);
            System.out.println(strParseLocalDateTime);
        } catch (DateTimeParseException e) {
            e.printStackTrace();
        }


        BillDTO billDTO = new BillDTO(amount, strParseLocalDateTime, categoryId, remark, type, userId);
        billService.addBill(billDTO);
        return Result.Success("添加成功");
    }
    @GetMapping("/bill/monthly/list")
    public Result list(@RequestParam("userId")  Integer userId,@RequestParam("yearMonth") String yearMonth,@RequestParam(value = "categoryId",required = false) Integer categoryId){
                List<Data> dataList=billService.list(userId,yearMonth,categoryId);
                return Result.Success(dataList);
    }
    @GetMapping("/bill/monthly/sum")
    public Result billAmountCount(@RequestParam(name="userId",required = true) Integer userId,
                                  @RequestParam(name = "yearMonth",required = true) String yearMonth,
                                  @RequestParam(name = "categoryId",required = false) Integer categoryId
                                  ){
        log.info("参数：:{},{},{}", userId, yearMonth, categoryId);
        ExpensesIncome expensesIncome = billService.billAmountCount(userId, yearMonth, categoryId);

        return Result.Success(expensesIncome);

    }
    @GetMapping("/bill/dtl")
    public Result getById(@RequestParam(name="billId",required = true) Integer billId){
        log.info("参数：:{}", billId);
        Bill bill = billService.getById(billId);
        return Result.Success(bill);
    }

    /**
     * 修改账单
     * @return
     */
    @PutMapping("/bill")
    public Result alterBill(@RequestParam("amount") Double amount, @RequestParam("billId") Integer billId, @RequestParam("billTime") String billTime, @RequestParam("categoryId") Integer categoryId, @RequestParam("type") String type,@RequestParam("remark") String remark){
        AlterBillDTO alterBill=new AlterBillDTO(amount, billId, billTime, categoryId, type, remark);
        billService.alterBill(alterBill);
        return Result.success();
    }

    /**
     *删除账单
     * @param billId
     * @return
     */
    @DeleteMapping("/bill")
    public Result deleteBill(@RequestParam("billId") Integer billId){
        billService.deleteBill(billId);
        return Result.success();
    }
    @GetMapping("/bill/monthly/statistics")
    public Result billMonthlyList(@RequestParam("type") String type,@RequestParam("userId") Integer userId,@RequestParam("yearMonth") String yearMonth){
        List<BillMonthlyListObject> billMonthlyClassify;
        String year = yearMonth.substring(0,4);
        String month = yearMonth.substring(5,7);
        billMonthlyClassify = billService.selectAmountGroupByName(type,userId,year,month);
        return Result.Success(billMonthlyClassify);
    }
}
